<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------

namespace Home\Controller;

use OT\DataDictionary;

/**
 * 前台首页控制器
 * 主要获取首页聚合数据
 */
class IndexController extends HomeController
{

    //系统首页
    public function index($page=1)
    {
        if(!is_login()){
            redirect(U('help/index/index'));
        }
        $list_all=S('friends_info_'.is_login());
        if(!$list_all){
            $map['who_follow'] = is_login();
            $follow = D('Follow')->where($map)->field('follow_who')->select();
            $uids = getSubByKey($follow, 'follow_who');
            $help = D('Help/Help')->getPage(array('uid' => array('in', $uids)),$tc,1,100,'create_time desc');
            $question = D('Question/Question')->getList(array('uid' => array('in', $uids),'is_repost'=>0),$ttc,1,'create_time desc',100);
            $answer_question=D('Question/QuestionAnswer')->answeredQuestionUids($uids,100);
            $help_attend=D('Help/HelpAttend')->getListFriendsAttend($uids,100);
            foreach ($help as &$v) {
                $v['flag'] = 'help';
            }
            unset($v);
            foreach ($question as &$v) {
                $v['flag'] = 'question';
            }
            unset($v);
            foreach($answer_question as &$v){
                $v['flag'] = 'answer';
            }
            unset($v);
            foreach ($help_attend as &$v) {
                $v['flag'] = 'attend';
            }
            unset($v);
            $list = array_merge($help, $question,$answer_question,$help_attend);
            $list = list_sort_by($list, 'create_time', 'desc');
            $list_all['list']=$list;
            $list_all['totalCount']=count($list);
            S('friends_info_'.is_login(),$list_all,60);
        }else{
            $list=$list_all['list'];
            $totalCount=$list_all['totalCount'];
        }
        $list = array_slice($list,($page-1)*50,50);
        $this->assign('list', $list);
        $this->assign('totalCount',$totalCount);
        $this->setTitle('首页');
        $this->display();
    }

    public function yiqipaipian()
    {
        $this->display();
    }

    public function getMyFriend(){
       $data =  R('SinaFriend/render',array(),'Widget');
        $this->ajaxReturn($data);
    }

    private function assignPostCount()
    {
        $post_count = S('home_post_count');
        if (empty($post_count)) {
            $post_count = D('ForumPost')->where(array('status' => 1))->count();
            S('home_post_count', $post_count, 600);
        }
        $this->assign('post_count', $post_count);
    }

    private function assignWeiboCount()
    {
        $weibo_count = S('home_weibo_count');
        if (empty($weibo_count)) {
            $weibo_count = D('Weibo')->where(array('status' => 1))->count();
            S('home_weibo_count', $weibo_count, 600);
        }
        $this->assign('weibo_count', $weibo_count);
    }

    private function assignEventCount()
    {
        $event_count = S('home_event_count');
        if (empty($event_count)) {
            $event_count = D('Event')->where(array('status' => 1))->count();
            S('home_event_count', $event_count, 600);
        }
        $this->assign('event_count', $event_count);
    }

    private function assignEvents()
    {
        $events = S('home_events');
        if (empty($events)) {
            $events = D('Event')->where(array('status' => 1, 'create_time > ' . (time() - 604800)))->order('reply_count')->limit(3)->select();
            S('home_events', $events, 600);
        }
        $this->assign('events', $events);
    }

    private function assignPosts()
    {
        $data = S('home_posts');
        if (empty($events)) {
            $data = D('ForumPost')->where(array('status' => 1, 'create_time > ' . (time() - 604800)))->order('reply_count')->limit(4)->select();
            S('home_posts', $data, 600);
        }
        $this->assign('posts', $data);
    }

    private function assignWeibos()
    {
        $data = S('home_weibos');
        if (empty($events)) {
            $data = D('Weibo')->where(array('status' => 1, 'create_time > ' . (time() - 604800)))->order('comment_count')->limit(4)->select();
            S('home_weibos', $data, 600);
        }
        $this->assign('weibos', $data);
    }

}